//submit tags and select associated accounts
var all_retweet_content;
$(document).ready(function() {
    $("#retweet_content").change(count_retweet_content_number);
    $("#submit-btn").click(submit_accounts);
});

function count_retweet_content_number() {
    all_retweet_content = $('textarea:#retweet_content').val().split('&');
    var retweet_content_number = all_retweet_content.length;
    $("#left_space").empty();
    $("#left_space").append(retweet_content_number);
}

function submit_accounts() {
    try {
        var tweet_id = $("input[name='tweet-id']").val();
        var selected_accounts = $(".checkbox:checked").parent().parent();
        var tweet_link = $("#tweet_link").val();
        var former_retweet_count = $("#former_retweet_count").val();
        var former_comment_count = $("#former_comment_count").val();
        var retweet_content = $("#retweet_content").val();
        var tweet_link = $("#tweet_link").val();
        var account_id = $("#account_id").val();
        var used_accounts_amount = $("#total_accounts").text();
        var used_followers_amount = $("#total_fans").text();
        var tweet_info = sprintf('tweet_link=%s&former_retweet_count=%s&former_comment_count=%s&used_accounts_amount=%s&used_followers_amount=%s', tweet_link, former_retweet_count, former_comment_count, used_accounts_amount, used_followers_amount);
        if (tweet_id == '' || tweet_link == '' || former_retweet_count == '' || former_comment_count == '') {
            alert('必要信息没有填全');
            return false;
        };
        var user_ids = [];
        $(selected_accounts).each(function(index, elem) {
            user_ids.push($(elem).attr("user_id"));
        });

        var proceed = confirm("Will submit " + $(user_ids).size() + " accounts, proceed?");

        if (proceed) {
            $.ajax({
                url: "../tools/submit_custom_task.php",
                type: "POST",
                data: {
                    user_ids: user_ids,
                    tweet_id: tweet_id,
                    retweet_content: all_retweet_content,
                    tweet_info: tweet_info 
                },
                success: function(data) {
                    alert(data);
                },
                error: function(data) {
                    alert("Ajax submit failed, status:" + data.statusText);
                    console.log(data);
                }
            });        
        }
    } catch (err) {
        alert("Uncaught fucking exception: " + err);
    }
}

function check_or_uncheck_multiple_accounts(clicked_checkbox) {
    if(clicked_checkbox.checked == true) {
        $("#" + clicked_checkbox.id).parent().parent().parent().children().children().children(".checkbox:enabled").attr("checked", true);
    }            
    else {
        $("#" + clicked_checkbox.id).parent().parent().parent().children().children().children(".checkbox:enabled").attr("checked", false);
    }
}

function add_tag_and_account(event) {
    current = new Date();
    seconds = current.getTime();
    add_tag(event);
    add_accounts(event);
}

//show accounts associated to tags
function add_accounts(event) {
    if(event.which == 13) {
        minimum_followers_amount = $("#minimum_followers_amount").val();
        maximum_followers_amount = $("#maximum_followers_amount").val();
        $("#waiting-icon").show();
        account_tag = $(event.target).val();
        account_div = sprintf('<table border="1" class="%1$s" id="account%1$s"><br /></table>', seconds);
        $("#show-accounts").prepend(account_div);
        if(account_tag == "give me all") {
            $.getJSON("../tools/id-by-tag.php?minimum_followers_amount=" + minimum_followers_amount + "&maximum_followers_amount=" + maximum_followers_amount, function(account) {
                rotate_each_json(account);
            });
        }
        else {
            $.getJSON("../tools/id-by-tag.php?tag=" + account_tag + "&minimum_followers_amount=" + minimum_followers_amount + "&maximum_followers_amount=" + maximum_followers_amount, function(account) {
                rotate_each_json(account);
            });
        }
    }
}

function rotate_each_json(account) {
    $.each(account,function(index,account_info) {
        console.log(account_info);
        var user_id = account_info.user_id;
        var local_id = account_info.local_id;
        var tweet_count = account_info.tweet_count;
        var follow_count = account_info.follow_count;
        var screen_name = account_info.screen_name;
        var followed_count = account_info.followed_count;
        var user_link = 'http://weibo.com/' + local_id;
        var accounts_info = sprintf('<tr id="tr_%6$s_%5$s" user_id="%7$s" class="accounts_info"><td class="accounts_checkbox"><input id="input_%6$s_%5$s" class="checkbox" type="checkbox" checked="checked" onclick="click_check_box()"/></td><td class="accounts_name"><a class="account_link" href="%1$s" target="blank">%8$s</a></td><td id="fans_count" class="account_detail">%2$s</td><td class="account_detail">%3$s</td><td class="account_detail">%4$s</td></tr>', user_link, followed_count, follow_count, tweet_count, seconds, local_id, user_id, screen_name);  
            $("#account" + seconds).prepend(accounts_info);
        });
    var account_title = sprintf('<tr class="show-accounts"><th><input id="checkbox_title_%1$s" type="checkbox" checked="checked" onclick="check_or_uncheck_multiple_accounts(this), selected_fans_accounts()" /></th><th class="account_name">%2$s相关帐号<a class="delete_tag_account" href="#" onclick="remove_tag_account(%1$s)">X</a></th><th class="account_detail">粉丝数</th><th class="account_detail">关注数</th><th class="account_detail">发文数</th></tr>', seconds, account_tag);
    $("#account" + seconds).prepend(account_title);
    check_repeat();
    selected_fans_accounts();
    get_user_id();
    $("#waiting-icon").hide();
}

//check repeat acount
function check_repeat() {
    var accounts_number = $(".accounts_info").length;
    var accounts_id = new Array();
    var accounts_uid = new Array();
    var accounts_checkbox = new Array(); 
    $(".accounts_info").each(function(index) {
        accounts_id.push($(this).attr("id"));
        accounts_id_string = new String(accounts_id[index]);
        accounts_id_split = accounts_id_string.split("_");
        accounts_uid.push(accounts_id_split[1]);
    });
    for (var i=accounts_number-1;i>0;i--) {  
        for (var j=0;j<i;j++) {
            if(accounts_uid[i] == accounts_uid[j]) {
                $('#' + accounts_id[j]).css({"text-decoration":"line-through", "background-color":"c0c0c0"});
                $('#' + accounts_id[j]).children($(".accounts_checkbox")).children().attr({"checked":false,"disabled":"disabled"});
            }
        }
    }
};

//add tag to dl
function add_tag(event) {
    if (event.which == 13) {
        var tag = $(event.target).val();
        var tags = sprintf('<dt class="%1$s" id="tag%1$s" class="tags">%2$s<a class="delet_tag_account" href="#" onclick="remove_tag_account(%1$s)">X</a></dt>', seconds, tag);
        $("#submitted-tags").prepend(tags);
    }
};
//remove specified tag and account
function remove_tag_account(target) {
    $("." + target).remove();
    $('.accounts_info').css({"text-decoration":"none", "background-color":"transparent"});
    $('.checkbox:disabled').attr("checked","checked");
    $('.checkbox').removeAttr("disabled");
    check_repeat();
    selected_fans_accounts();
    get_user_id();
}

//sum selected number of fans and accounts
function selected_fans_accounts() {
    $("#total_accounts").empty();
    $("#total_fans").empty();
    var selected_accounts = $(".checkbox:checked").length;
    $("#total_accounts").append(selected_accounts);
    var total_fans_account = 0;
    $(".checkbox:checked").each(function(index,checked_checkbox) {
        var input_td = $(this).parent().parent().children("#fans_count").text();
        total_fans_account += input_td*1;
    });
    $("#total_fans").append(total_fans_account);
}

//get selected user's email
function get_user_id() {
    var user_id_array = new Array();
    $(".checkbox:checked").each(function(index,checked_checkbox) {
        user_id_array[index] = $(this).parent().parent().attr("user_id");
    });
}

//click checkbox event
function click_check_box() {
    selected_fans_accounts();
    get_user_id();
}

function resize_show_accounts() {
var client_height = parseInt(document.body.clientHeight);
$("#show-accounts").height(client_height*0.7);
}

    
